package in.muton.edushare.grading.panels.prevgrades;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.cp.GradeConfirmPage;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.AcademicYearModelDAO;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Universities;

public class PrevCourseGradePanel extends EdusharePanel {
	private static final long serialVersionUID = 198938024195133911L;
	private IModel<Universities> univModel;
	private GenericRenderer<Integer> grYear;
	private GenericRenderer<IModel<AcademicYearModel>> grAcad;
	private Integer batchYear;
	protected IModel<FilesUploaded> fileModel;
	private GeneralFileUpload gfu;

	public PrevCourseGradePanel(String id, IModel<Universities> model,
			EduShareBasePage page) {
		super(id, model, page);
		this.univModel = model;
		Form<Void> f = new Form<Void>("prevGradeForm") {
			@Override
			public void onSubmit() {
				gfu.formSubmit();
				pgpPanelSubmit();
			}

			private static final long serialVersionUID = 2176554783733460967L;
		};
		add(f);
		f.add(gfu = new GeneralFileUpload("fupload", page) {
			private static final long serialVersionUID = 7172791289564037860L;

			@Override
			public void processFile(IModel<FilesUploaded> fileModel) {
				PrevCourseGradePanel.this.fileModel = fileModel;
			}
		});

		grYear = new GenericRenderer<Integer>(getYearMap(), batchYear);
		grAcad = new GenericRenderer<IModel<AcademicYearModel>>(getAcadMap(),
				null);
		grYear.sort(false);
		grAcad.sort(true);
		f.add(new DropDownChoice<String>("year", grYear.getModel(), grYear
				.getOptions(), grYear));
		f.add(new DropDownChoice<String>("semester", grAcad.getModel(), grAcad
				.getOptions(), grAcad));
	}

	private void pgpPanelSubmit() {
		if (grYear.getSelection() == null || grAcad.getSelection() == null) {
			setError("Academic year and semester are required fields.");
			return;
		}
		int currentSem = new AcademicYearModelDAO(getORMInterface(), univModel
				.getObject()).getActiveSemester().getSequenceNo();
		if (grYear.getSelection().intValue() == univModel.getObject()
				.getCurrentAcadYear().intValue()) {
			AcademicYearModel aym = grAcad.getSelection().getObject();
			if (aym.getSequenceNo().intValue() > currentSem) {
				setError("You can upload grades for current or past semesters."
						+ "<br/> Grades for future semesters are not allowed.");
				return;
			}
		}
		if (fileModel == null) {
			setError("The excel file with student grades has to be "
					+ "uploaded in order to proceed");
			return;
		}
		setResponsePage(new GradeConfirmPage(univModel, fileModel, grYear
				.getSelection(), grAcad.getSelection()));
	}

	private HashMap<String, Integer> getYearMap() {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		int currYear = univModel.getObject().getCurrentAcadYear();
		int firstYear = currYear - 8;
		for (int i = currYear; i >= firstYear; i--) {
			map.put(Integer.toString(i), i);
		}
		return map;
	}

	private HashMap<String, IModel<AcademicYearModel>> getAcadMap() {
		HashMap<String, IModel<AcademicYearModel>> map = new HashMap<String, IModel<AcademicYearModel>>();
		List<AcademicYearModel> acad = univModel.getObject()
				.getAcademicYearModels();
		for (AcademicYearModel a : acad) {
			map
					.put(a.getSemesterName(),
							new CayenneModel<AcademicYearModel>(a));
		}
		return map;
	}

}
